package com.ny.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class DatabaseInit {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Value("${spring.mail.username}")
    private String mailUsername;
    
    /**
     * 应用启动时更新admin用户的邮箱为系统邮箱
     */
    @PostConstruct
    public void initAdminEmail() {
        try {
            String sql = "UPDATE user SET email = ? WHERE username = 'admin'";
            int rowsAffected = jdbcTemplate.update(sql, mailUsername);
            if (rowsAffected > 0) {
                System.out.println("成功将admin用户的邮箱更新为: " + mailUsername);
            } else {
                System.out.println("未找到admin用户或更新失败");
            }
        } catch (Exception e) {
            System.err.println("初始化数据库邮箱失败: " + e.getMessage());
        }
    }
}